﻿using Function;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.Security;
using UI.Permission;
using UI.UserControl.Base;

namespace UI.UserControl.Base
{
    public class SingleActionEntity : System.Web.UI.UserControl
    {
        #region 页面链接
        /// <summary>
        /// 实体id 必须设置
        /// </summary>
        public string Entityid { get; set; }

        private Boolean _autoAppendId = true;
        /// <summary>
        /// 是否自动在页面末尾加上 "?entityid=" 和 mid 传参
        /// </summary>
        public Boolean AutoAppendId { get { return _autoAppendId; } set { _autoAppendId = value; } }

        /// <summary>
        /// 页码
        /// </summary>
        public string PageIndex = HttpContext.Current.Request.QueryString["PageIndex"] ?? "1";

        private string _relativePathDepth = Method.RelativePathDepth;
        /// <summary>
        /// 相对深度
        /// </summary>
        public string RelativePathDepth { get { return _relativePathDepth; } set { _relativePathDepth = value; } }

        private string _editPage = "Edit.aspx";

        /// <summary>
        /// 编辑页面 默认Edit.aspx
        /// </summary>
        public string EditPage { internal protected get { return AutoAppendId ? _editPage += "?entityid=" + Entityid + "&mid=" + (Request.QueryString["mid"] ?? "") + "&PageIndex=" + PageIndex : _editPage; } set { _editPage = value; } }

        private string _deletePage = "Delete.aspx";
        /// <summary>
        /// 删除页面 默认Delete.aspx
        /// </summary>
        public string DeletePage { internal protected get { return AutoAppendId ? _deletePage += "?entityid=" + Entityid + "&mid=" + (Request.QueryString["mid"] ?? "") + "&PageIndex=" + PageIndex : _deletePage; } set { _deletePage = value; } }

        private string _detailPage = "Detail.aspx";
        /// <summary>
        /// 详细页面 默认Detail.aspx
        /// </summary>
        public string DetailPage { internal protected get { return AutoAppendId ? _detailPage += "?entityid=" + Entityid + "&mid=" + (Request.QueryString["mid"] ?? "") + "&PageIndex=" + PageIndex : _detailPage; } set { _detailPage = value; } }

        #endregion

        #region 显示属性
        /// <summary>
        /// 数据绑定
        /// </summary>
        public string DataFiled { get; set; }

        private string _editDisplayStyle = "none";
        /// <summary>
        /// 编辑按钮显示属性
        /// </summary>
        public string EditDisplayStyle { get { return _editDisplayStyle; } protected set { _editDisplayStyle = value; } }

        private string _deleteDisplayStyle = "none";
        /// <summary>
        /// 删除按钮显示属性
        /// </summary>
        public string DeleteDisplayStyle { get { return _deleteDisplayStyle; } protected set { _deleteDisplayStyle = value; } }

        private string _viewDisplayStyle = "none";
        /// <summary>
        /// 详细按钮显示属性
        /// </summary>
        public string ViewDisplayStyle { get { return _viewDisplayStyle; } protected set { _viewDisplayStyle = value; } }

        private Boolean _editVisible = true;
        /// <summary>
        /// 是否显示编辑按钮
        /// </summary>
        public Boolean EditVisible { get { return _editVisible; } set { _editVisible = value; } }

        private Boolean _deleteVisible = true;
        /// <summary>
        /// 是否显示删除按钮
        /// </summary>
        public Boolean DeleteVisible { get { return _deleteVisible; } set { _deleteVisible = value; } }

        private Boolean _viewVisible = true;
        /// <summary>
        /// 是否显示详细按钮
        /// </summary>
        public Boolean ViewVisible { get { return _viewVisible; } set { _viewVisible = value; } }
        #endregion

        #region 按钮显示方式 按钮或者文本
        public enum ButtonShowType
        {
            /// <summary>
            /// 图片按钮
            /// </summary>
            Image = 0,

            /// <summary>
            /// 文本链接
            /// </summary>
            Text = 1,

            /// <summary>
            /// 图片文字同时显示
            /// </summary>
            ImageAndText = 2
        }
        /// <summary>
        /// 默认显示属性
        /// </summary>
        private static ButtonShowType defaultShowType = ButtonShowType.Text;

        private ButtonShowType _editButtonShowType = defaultShowType;
        /// <summary>
        /// 编辑按钮显示类型
        /// </summary>
        public ButtonShowType EditButtonShowType
        {
            get { return _editButtonShowType; }
            set { _editButtonShowType = value; }
        }
        private ButtonShowType _deleteButtonShowType = defaultShowType;
        /// <summary>
        /// 删除按钮显示类型
        /// </summary>
        public ButtonShowType DeleteButtonShowType
        {
            get { return _deleteButtonShowType; }
            set { _deleteButtonShowType = value; }
        }

        private ButtonShowType _detailButtonShowType = defaultShowType;
        /// <summary>
        /// 详细按钮显示类型
        /// </summary>
        public ButtonShowType DetailButtonShowType
        {
            get { return _detailButtonShowType; }
            set { _detailButtonShowType = value; }
        }

        private string _editButtonText = "编辑";
        /// <summary>
        /// 编辑按钮文本
        /// </summary>
        public string EditButtonText
        {
            get { return _editButtonText; }
            set { _editButtonText = value; }
        }

        private string _deleteButtonText = "删除";
        /// <summary>
        /// 删除按钮文本
        /// </summary>
        public string DeleteButtonText
        {
            get { return _deleteButtonText; }
            set { _deleteButtonText = value; }
        }

        private string _detailButtonText = "查看详细";
        /// <summary>
        /// 详细按钮文本
        /// </summary>
        public string DetailButtonText
        {
            get { return _detailButtonText; }
            set { _detailButtonText = value; }
        }

        /// <summary>
        /// 编辑按钮图片显示属性
        /// </summary>
        protected string EditImageDisplayProperty = "";
        /// <summary>
        /// 删除按钮图片显示属性
        /// </summary>
        protected string DeleteImageDisplayProperty = "";
        /// <summary>
        /// 详细按钮图片显示属性
        /// </summary>
        protected string DetailImageDisplayProperty = "";

        /// <summary>
        /// 全局显示属性
        /// </summary>
        private ButtonShowType _globalButtonShowType = defaultShowType;

        /// <summary>
        /// 全局显示属性
        /// </summary>
        public ButtonShowType GlobalButtonShowType
        {
            get { return _globalButtonShowType; }
            set { _globalButtonShowType = value; }
        }

        private Boolean _setAllButtonsShowType = false;
        /// <summary>
        /// 统一设置所有按钮的显示属性
        /// </summary>
        public Boolean SetAllButtonsShowType
        {
            get { return _setAllButtonsShowType; }
            set { _setAllButtonsShowType = value; }
        }

        protected internal Action Initcomponent;
        #endregion

        #region 操作相关
        private Boolean _confirmDelete = true;
        /// <summary>
        /// 确认删除
        /// </summary>
        public Boolean ConfirmDelete { get { return _confirmDelete; } set { _confirmDelete = value; } }

        private string _confirmDeleteMsg = "确定要删除吗";
        /// <summary>
        /// 删除提示信息
        /// </summary>
        public string ConfirmDeleteMsg { get { return _confirmDeleteMsg; } set { _confirmDeleteMsg = value; } }
        #endregion

        public SingleActionEntity()
        {
            Initcomponent += InitialButtons;
        }

        /// <summary>
        /// 初始化按钮
        /// </summary>
        private void InitialButtons()
        {
            //权限
            if (!RightCheck.HavePermission(RightCheck.permissionType.View))
            {
                ViewVisible = false;
            }
            if (!RightCheck.HavePermission(RightCheck.permissionType.Delete))
            {
                DeleteVisible = false;
            }
            if (!RightCheck.HavePermission(RightCheck.permissionType.Edit))
            {
                EditVisible = false;
            }
            //按钮显隐
            if (ViewVisible)
            {
                ViewDisplayStyle = "";
            }
            else
            {
                ViewDisplayStyle = "none";
            }
            if (EditVisible)
            {
                EditDisplayStyle = "";
            }
            else
            {
                EditDisplayStyle = "none";
            }
            if (DeleteVisible)
            {
                DeleteDisplayStyle = "";
            }
            else
            {
                DeleteDisplayStyle = "none";
            }

            if (SetAllButtonsShowType)
            {
                EditButtonShowType = DeleteButtonShowType = DetailButtonShowType = GlobalButtonShowType;
            }
            //编辑按钮
            switch (EditButtonShowType)
            {
                case ButtonShowType.Image:
                    //清空文字 显示图片
                    EditButtonText = "";
                    EditImageDisplayProperty = "";
                    break;
                case ButtonShowType.Text:
                    //隐藏图片 显示文字
                    EditImageDisplayProperty = "style=\"display:none\"";
                    break;
                case ButtonShowType.ImageAndText:
                    //默认 显示图片和文字
                    if (EditButtonText.IsEmpty())
                        EditButtonText = "编辑";
                    EditImageDisplayProperty = "";
                    break;
                default:
                    break;
            }

            //删除按钮
            switch (DeleteButtonShowType)
            {
                case ButtonShowType.Image:
                    //清空文字 显示图片
                    DeleteButtonText = "";
                    DeleteImageDisplayProperty = "";
                    break;
                case ButtonShowType.Text:
                    //隐藏图片 显示文字
                    DeleteImageDisplayProperty = "style=\"display:none\"";
                    break;
                case ButtonShowType.ImageAndText:
                    //默认 显示图片和文字
                    if (DeleteButtonText.IsEmpty())
                        DeleteButtonText = "删除";
                    DeleteImageDisplayProperty = "";
                    break;
                default:
                    break;
            }

            //详细按钮
            switch (DetailButtonShowType)
            {
                case ButtonShowType.Image:
                    //清空文字 显示图片
                    DetailButtonText = "";
                    DetailImageDisplayProperty = "";
                    break;
                case ButtonShowType.Text:
                    //隐藏图片 显示文字
                    DetailImageDisplayProperty = "style=\"display:none\"";
                    break;
                case ButtonShowType.ImageAndText:
                    //默认 显示图片和文字
                    if (DetailButtonText.IsEmpty())
                        DetailButtonText = "详细";
                    DetailImageDisplayProperty = "";
                    break;
                default:
                    break;
            }
        }
    }
}